package cn.ven.service;

import cn.ven.entity.Blog;
import cn.ven.entity.Collect;
import cn.ven.mapper.CollectMapper;
import cn.ven.tool.SessionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectServiceImpl implements CollectService{

    @Autowired(required = false)
    CollectMapper collectMapper;

    @Autowired
    BlogService blogService;

    @Override
    public int addCollect(Collect collect) {
        Collect collect1 = collectMapper.selectById(collect.getId());
        if (collect1==null){
            collect.setUid(SessionUtil.getId());
            collectMapper.insert(collect);
            Blog blog = blogService.findById(collect.getBlogid());
            blog.setCollectNum(blog.getCollectNum()+1);
            blogService.updateBlog(blog);
            return 1;
        }
        return 0;
    }

    @Override
    public int cancelCollect(int blogid) {
        LambdaQueryWrapper<Collect> queryWrapper =new LambdaQueryWrapper();
        queryWrapper.eq(Collect::getBlogid,blogid);
        queryWrapper.eq(Collect::getUid,SessionUtil.getId());
        Collect collect = collectMapper.selectOne(queryWrapper);
        if (collect!=null){
            collectMapper.deleteById(collect);
            Blog blog = blogService.findById(blogid);
            blog.setCollectNum(blog.getCollectNum()+1);
            blogService.updateBlog(blog);
            return 1;
        }
        return 0;
    }


}
